package com.edgclub.edg.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzTopicDetailActivity;
import com.edgclub.edg.model.RecommendTopic;
import com.edgclub.edg.model.Topic;
import com.edgclub.edg.model.User;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.DensityUtils;

import java.util.ArrayList;

/**
 */
public class FindCartoonAdapter extends BaseAdapter {
    private Context mContext;

    private ArrayList<RecommendTopic> datas;

    private GlideUtils glideUtils;

    private ViewHolder holder;
    private int itemW;

    public FindCartoonAdapter(Context mContext) {
        this.mContext = mContext;
        glideUtils = new GlideUtils();
        itemW = (DensityUtils.getScreenW((Activity) mContext)-33)/2;
    }

    public ArrayList<RecommendTopic> getDatas() {
        return datas;
    }

    public void setDatas(ArrayList<RecommendTopic> datas) {
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        if (null == convertView) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_find_cartoon_grid, null);
            holder = new ViewHolder();
            holder.video_icon = (ImageView) convertView.findViewById(R.id.video_icon);
            holder.user_icon = (ImageView) convertView.findViewById(R.id.user_icon);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.user_name = (TextView) convertView.findViewById(R.id.user_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final RecommendTopic recommendTopic=datas.get(position);
        if(null!=recommendTopic&&null!=recommendTopic.getPosts()){
            final Topic topic=recommendTopic.getPosts();
            User user=topic.getUser();
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) holder.video_icon.getLayoutParams();
            lp.width = itemW;
            holder.video_icon.setLayoutParams(lp);
            if(!TextUtils.isEmpty(recommendTopic.getCover())){
                glideUtils.Glidde_loadRoudImage(mContext,recommendTopic.getCover(),holder.video_icon,20);
            }else{
                if(topic.getAlbums()!=null && topic.getAlbums().size()>0){
                    glideUtils.Glidde_loadRoudImage(mContext,topic.getAlbums().get(0).getThumbnail(),holder.video_icon,20);
                }
            }
            glideUtils.Glidde_loadCropCircleImage(mContext,user.getLogo(),holder.user_icon);
            if(TextUtils.isEmpty(recommendTopic.getTitle())){
                holder.tv_title.setText(recommendTopic.getPosts().getContent()+"");
            }else{
                holder.tv_title.setText(recommendTopic.getTitle()+"");
            }
            holder.user_name.setText(user.getNick());
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, GzTopicDetailActivity.class);
                    intent.putExtra("topicId", topic.getPostsId());
                    mContext.startActivity(intent);
                    ((Activity)mContext).overridePendingTransition(R.anim.slide_in_from_right,
                            R.anim.slide_out_to_left);
                }
            });
        }
        return convertView;
    }


    public class ViewHolder {

        public ImageView video_icon ,user_icon ;
        public TextView tv_title ,user_name;
    }


}
